﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;

public partial class Media_Regions : System.Web.UI.Page
{
    private enum Views
    {
        MainView = 0,
        AddRegionView = 1
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ApplyAuthorizationRules();
        }
    }

    protected void lbtnAddRegion_Click(object sender, EventArgs e)
    {
        ChangeViews();
    }

    private void ApplyAuthorizationRules()
    {
        this.RegionGridView.Columns[RegionGridView.Columns.Count - 1].Visible =
            Regions.CanEditObject();
        this.lbtnAddRegion.Visible = Regions.CanAddObject();
    }

    private void ChangeViews()
    {
        if (this.MultiView1.ActiveViewIndex == (int)Views.MainView)
        {
            this.lbtnAddRegion.Text = "返  回";
            this.MultiView1.ActiveViewIndex = (int)Views.AddRegionView;
        }
        else
        {
            this.lbtnAddRegion.Text = "添加区域";
            this.MultiView1.ActiveViewIndex = (int)Views.MainView;
        }
    }

    #region BusinessObject

    private Regions GetRegions()
    {
        object businessObj = Session["CurrentObj"];
        if (businessObj == null
            || !(businessObj is Regions))
        {
            try
            {
                businessObj = Regions.GetRegions();
                Session["CurrentObj"] = businessObj;
            }
            catch (System.Security.SecurityException)
            {
                Response.Redirect("~/Media");
            }
        }
        return businessObj as Regions;
    }

    private int SaveRegions(Regions regions)
    {
        int rowsAffected = 0;
        try
        {
            Session["CurrentObj"] = regions.Save();
            rowsAffected = 1;
        }
        catch (Csla.Validation.ValidationException ex)
        {
            throw;
        }
        catch (Csla.DataPortalException ex)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw;
        }
        return rowsAffected;
    }

    #endregion


    #region RegionsDataSource

    protected void RegionsDataSource_DeleteObject(object sender, Csla.Web.DeleteObjectArgs e)
    {
        Regions regions = GetRegions();
        regions.Remove(int.Parse(e.Keys["Id"].ToString()));
        e.RowsAffected = SaveRegions(regions);
    }

    protected void RegionsDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = GetRegions();
    }

    protected void RegionsDataSource_UpdateObject(object sender, Csla.Web.UpdateObjectArgs e)
    {
        Regions regions = GetRegions();
        Region region = regions.GetRegionById(int.Parse(e.Keys["Id"].ToString()));
        region.Name = e.Values["Name"] as string;
        region.Description = e.Values["Description"] as string;
        e.RowsAffected = SaveRegions(regions);
    }

    #endregion

    #region RegionDataSource

    protected void RegionDataSource_InsertObject(object sender, Csla.Web.InsertObjectArgs e)
    {
        Regions regions = GetRegions();
        Region region = regions.AddNew();
        region.Name = e.Values["Name"] as string;
        region.Description = e.Values["Description"] as string;
        e.RowsAffected = SaveRegions(regions);
        this.RegionGridView.DataBind();
        ChangeViews();
    }

    #endregion
    
}
